package edu.vtc.clacky;

import java.util.ArrayList;
import java.util.Scanner;

/**
 * Clacky is a four function calculator that manipulates an ArrayList of
 * Rational. Additional commands are in place to run methods from the
 * StackOperations class, along with the four typical operators.
 * 
 * @author Tara Gallagher
 *
 */
public class Clacky {

	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);

		// Initialize an InMemoryStorage object to store values in.
		InMemoryStorage registers = new InMemoryStorage();

		// Create and initialize the parameter stack.
		ArrayList<Rational> numbers = new ArrayList<Rational>();
		for (int i = 0; i < 4; i++) {
			numbers.add(new Rational(0, 1));
		}

		while (true) {
			try {
				// Display the current numbers within the stack.
				for (int i = numbers.size() - 1; i >= 0; --i) {
					System.out.println(numbers.get(i));
				}

				// Interpret a command line.
				System.out.print("> ");
				String commandLine = input.nextLine();
				// Allow the use to exit the program.
				if (commandLine.equals("quit")) {
					input.close();
					System.exit(0);
				}

				// Process each word on the command line.
				String[] commandWords = commandLine.split("\\s+");
				for (String word : commandWords) {

					switch (word) {

					case "+":
						Rational sum = numbers.get(0).add(numbers.get(1));
						numbers.remove(1);
						numbers.set(0, sum);
						break;

					case "-":
						Rational difference= numbers.get(1).subtract(numbers.get(0));
						numbers.remove(1);
						numbers.set(0, difference);
						break;

					case "*":
						Rational product = numbers.get(0).multiply(numbers.get(1));
						numbers.remove(1);
						numbers.set(0, product);
						break;

					case "/":
						Rational quotient = numbers.get(1).divide(numbers.get(0));
						numbers.remove(1);
						numbers.set(0, quotient);
						break;

					case "dup":
						ClackStack.dup(numbers);
						break;

					case "pick":
						Rational j = numbers.get(0);
						numbers.remove(0);
						ClackStack.pick(numbers, j);
						break;

					case "rollup":
						Rational k = numbers.get(0);
						numbers.remove(0);
						ClackStack.rollUp(numbers, k);
						break;

					case "rolldown":
						Rational l = numbers.get(0);
						numbers.remove(0);
						ClackStack.rollDown(numbers, l);
						break;

					case "swap":
						ClackStack.swap(numbers);
						break;

					case "store":
						Rational m = numbers.get(0);
						numbers.remove(0);
						Rational n = numbers.get(0);
						numbers.remove(0);

						// Convert the Rational to an integer.
						// TODO Make a constructor in class Rational to convert
						// integers (?).
						String mString = m.toString();
						int mInt = Integer.parseInt(mString);
						
						
							registers.store(mInt, n);
						
						break;
						
					case "recall":
						Rational o = numbers.get(0);
						numbers.remove(0);

						// Convert the Rational to an integer.
						// TODO Make a method in class Rational to convert to
						// integers (?).
						String oString = o.toString();
						int oInt = Integer.parseInt(oString);
				
						numbers.add(0, registers.recall(oInt));
						break;
						
					case "registercount":
						numbers.add(0, registers.registerCount());
						break;

					default:
						Rational value = new Rational(word);
						numbers.add(0, value);

				}

			}
				} catch (BadRegisterException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
		}

	}
}
